package com.cy.controller;

import com.cy.controller.ex.*;
import com.cy.service.ex.*;
import com.cy.utils.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

/**
 * 异常处理
 */
public class BaseController {
    //success
    public static final int OK=200;

    //用于统一处理抛出的异常
    //自动将异常对象传到这个方法的参数列表上
    //当项目产生异常会被统一拦截到此方法方法返回值直接给前端
    @ExceptionHandler({ServiceException.class,FileUploadException.class})
    public JsonResult<Void> handlerException(Throwable e)
    {
        JsonResult<Void> result=new JsonResult<>();
        if(e instanceof UsernameDuplicatedException)
        {
            result.setState(4000);
            result.setMessage("用户名被占用");
        }
        else if(e instanceof UserNotFoundException)
        {
            result.setState(4001);
            result.setMessage("用户不存在异常");
        }
        else if(e instanceof PasswordNotMatchException)
        {
            result.setState(4002);
            result.setMessage("密码错误在异常");
        }
        else if(e instanceof AddressCountLimitException){
            result.setState(4003);
            result.setMessage("用户地址超出上限");
        }
        else if(e instanceof AddressNotFoundException)
        {
            result.setState(4004);
            result.setMessage("用户收获地址找不到异常");

        }
        else if(e instanceof AccessDeniedException)
        {
            result.setState(4005);
            result.setMessage("数据非法访问");

        }
        else if(e instanceof ProductNotFindException)
        {
            result.setState(4006);
            result.setMessage("商品无法寻找");

        }
        else if(e instanceof CarNotFoundException)
        {
            result.setState(4007);
            result.setMessage("购物车数据不存在");

        }
        else if(e instanceof InsertException)
        {
            result.setState(5000);
            result.setMessage("未知异常");
        }else if(e instanceof UpdateException)
        {
            result.setState(5001);
            result.setMessage("更新数据产生异常");

        }else if(e instanceof DeleteException)
        {
            result.setState(5001);
            result.setMessage("删除数据产生异常");

        }
        else if (e instanceof FileEmptyException) {
            result.setState(6000);
        } else if (e instanceof FileSizeException) {
            result.setState(6001);
        } else if (e instanceof FileTypeException) {
            result.setState(6002);
        } else if (e instanceof FileStateException) {
            result.setState(6003);
        } else if (e instanceof FileUploadIOException) {
            result.setState(6004);
        }


        return result;
    }

    /**
     * 获取session对象UID
     * @param session
     * @return 当前用户UID的值
     */
    protected final Integer getuidFromSession(HttpSession session)
    {
           return  Integer.valueOf(session.getAttribute("uid").toString());
    }

    protected final String getUsernameFromSession(HttpSession session){
        return session.getAttribute("username").toString();
    }




}
